<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JS桥测试</title>
</head>
<body>

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
    {{ message }}
<p></p>

<button @click="pushPage()">新建窗口</button>
<p></p>

<button @click="popPage">关闭窗口</button>
<p></p>

<button @click="flashOn">打开闪光灯</button>
<p></p>

<button @click="flashOff">关闭闪光灯</button>
<p></p>

<button @click="vibrationOn">马达振动</button>
<p></p>

<button @click="dialPhone">拨打电话</button>
<p></p>

<button @click="scanQRCode">扫二维码</button>
<p></p>

</div>


</body>

</html>

<script>

var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  },
  mounted:function(){
    window['callJS'] = (message) => {
      this.callJS(message);
    }
   },
  methods:{
    callJS: function (message) {
      this.message = message;
    },
    pushPage:function(){
        console.log('pushPage');
        JSBridge.postMessage('{"func":"navigator","action":"push","title":"SomeTitle","url":"http://emberwei.gitee.io/js_bridge_test_page/"}');
    },
    popPage:function(){
        console.log('popPage');
        JSBridge.postMessage('{"func":"navigator","action":"pop"}');
    },
    flashOn:function(){
        JSBridge.postMessage('{"func":"flash","action":"on"}');
    },
    flashOff:function(){
        JSBridge.postMessage('{"func":"flash","action":"off"}');
    },
    vibrationOn:function(){
        JSBridge.postMessage('{"func":"vibration","action":"on"}');
    },
    dialPhone:function(){
        console.log('dialPhone');
        JSBridge.postMessage('{"func":"launcher","action":"dial","phone":"18046012345"}');
    },
    scanQRCode:function(){
        JSBridge.postMessage('{"func":"qrcode","action":"scan"}');
    },
  }
})


var SHAKE_SPEED_THRESHOLD = 300;
        var lastTime = 0;
        var x = 0;var y = 0;var z = 0;
        var lastX = 0 ;var lastY = 0;var lastZ = 0;

        function motionHandler(evt){
            var acceleration = evt.accelerationIncludingGravity;
            var curTime = Date.now();
            if((curTime - lastTime) > 120){
                var diffTime = curTime - lastTime;
                lastTime = curTime;
                x = acceleration.x;
                y = acceleration.y;
                z = acceleration.z;
                var speed = Math.abs(x + y + z - lastX - lastY - lastZ) / diffTime * 1000;
                if(speed > SHAKE_SPEED_THRESHOLD){
                    alert("你摇动了手机");
                    document.body.style.backgroundColor = red;
                }
                lastX = x ;
                lastY = y ;
                lastZ = z ;
            }
        }
        console.log(window)
        if(window.DeviceMotionEvent){
            window.addEventListener('devicemotion',motionHandler,true);
        }else{
            alert("您的设备不支持位置感应");
        }
</script>